<?php
/**
 * Created by Silent Night.
 * Author: Silent Night
 * email: silentwolf_wp@163.com
 * Date: 2017/9/28
 * Time: 9:19
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class Hosparticle extends Controller
{
    private $_model;
    private $title_field = [
    ];
    public $pagesize = 10;

    public $allTypeLists = array();

    public function _initialize()
    {
        $this->_model = new \app\admin\model\HospArticle();
        $this->assign('LoginUserInfo', $this->LoginUserInfo);

        //获取当前用户的权限信息
        $this->assign('ruleLists', $this->LoginUserInfo['CurrPower']);
        //获取文章大的分类
        $bigTypeLists = getHospBigTypeArticle();
        $this->assign('bigTypeLists', changeKeyForList($bigTypeLists,'id'));

    }

    private function getAllNotesType()
    {
        //获取自然音类型并分配给模板
        $this->allTypeLists = getAllHospArticleType();
        $this->allTypeLists = changeArrayKey($this->allTypeLists, 'id');
        $this->allTypeLists = makeTreeSelectData($this->allTypeLists);
    
        $this->assign('allTypeLists', $this->allTypeLists);
    }

    public function shangData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $res = $this->_model->updateData(array('stats' => 0), $id);
        if ($res || $res === 0) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }
    }

    public function xiaData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $res = $this->_model->updateData(array('stats' => 1), $id);

        if ($res || $res === 0) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }
    }

    /**
     * 列表页
     * @return mixed
     */
    public function index()
    {
        return $this->fetch('index');
    }


    public function lists()
    {
        $ser_key = trim(input("request.ser_key"));//auth
        $ser_val = trim(input("request.ser_val"));//auth

        $ser_pid = trim(input("request.ser_pid"));//auth
        $ser_hot = trim(input("request.ser_hot"));//auth
        $ser_stats = trim(input("request.ser_stats"));//auth
        $ser_bigtype = trim(input("request.ser_bigtype"));//auth

        if ($ser_key && $ser_val) {
            $where[$ser_key] = array('like', '%' . $ser_val . '%');
        }

        if($ser_bigtype){
            $where['bigtype'] = $ser_bigtype;
        }
        if ($ser_stats == '0' || $ser_stats == '1') {
            $where['stats'] = $ser_stats;
        }
        if ($ser_hot == '0' || $ser_hot == '1') {
            $where['hot'] = $ser_hot;
        }
        if ($ser_pid) {
            $where['pid'] = $ser_pid;
        }
        $where['is_delete'] = '0';

        $lists = $this->_model->lists($where, $this->pagesize);
        $lists['data'] = changeUidToUserinfoForLists($lists['data'], 'uid');
        $lists['data'] = filterHtmlForLists($lists['data']);
        $lists['data'] = (new \app\admin\mapper\Common())->listMapper($lists['data']);
        $this->assign('lists', $lists);
        $this->assign('request', input("request."));
//        $this->getAllNotesType();
        return view('lists');
    }

    /**
     * 删除
     * 支持批量删除和单条删除
     * @return mixed
     */
    public function delData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $res = $this->_model->updateDataForWhere(array('is_delete' => 1), array('id'=>$id,'is_allow_delete'=>0));
        if ($res) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }

    }


    /**
     * 删除
     * 支持批量删除和单条删除附件
     * @return mixed
     */
    public function delFilesData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $model = new \app\admin\model\NotesFiles();
        $res = $model->delData(array("id" => $id));
        if ($res) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }

    }

    /**
     * 文件排序
     * @return \think\response\Json
     */
    public function fileSorts()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        $sorts = trim(input("request.sorts"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        if (empty($sorts)) {
            $sorts = 0;
        }
        $model = new \app\admin\model\NotesFiles();
        $res = $model->updateData(array('sorts' => $sorts), $id);
        if ($res || $res == 0) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }

    }


    public function addData()
    {
        $sys_opt = trim(input("request.sys_opt"));//auth
        if ($sys_opt) {
            $data1 = $_REQUEST['data'];
            $conts = $_REQUEST['conts'];
            $relation_ids = $_REQUEST['relationInput'];
            $data2 = array(
                'uid' => $this->LoginUserInfo['UserInfo']['id'],
                'ctime' => time(),
                'content' => $conts,
                'relation_ids' => $relation_ids,
                'utime' => time(),
            );
            $data = array_merge($data1, $data2);
            $res = $this->_model->add($data);
            if ($res) {
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }
        $title = "添加";
        $this->getAllNotesType();
        $this->assign('title', $title);
        return view('addData');
    }

    public function infoData()
    {

        $id = trim(input("request.id"));//auth

        $info = $this->_model->getDataForWhere(array('id' => $id));

        $info['UserInfo'] = array();
        if ($info['uid']) {
            $info['UserInfo'] = getUserInfoForID($info['uid']);
        }
        $FileLists = getNotesFiles($id, 0, "sorts desc,id desc");//获取发帖附件数据
        $this->assign('FileLists', $FileLists);

        $this->getAllNotesType();
        $this->assign('info', $info);
        $title = "详情";
        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('infoData');

    }


    public function editData()
    {

        $id = trim(input("request.id"));//auth
        $sys_opt = trim(input("request.sys_opt"));//auth
        //查询用户
        $info = $this->_model->getDataForWhere(array('id' => $id));

        if ($sys_opt) {
            $data1 = $_REQUEST['data'];
            $conts = $_REQUEST['conts'];
            $relation_ids = $_REQUEST['relationInput'];
            $w = array(
                'id' => $id
            );
            $data2 = array(
                'utime' => time(),
                'content' => $conts,
                'relation_ids' => $relation_ids
            );
            $data = array_merge($data1, $data2);
            $res = $this->_model->updateDataForWhere($data, $w);

            if ($res || ($res === 0)) {

                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }

        $relationProductList = array();
        if($info['relation_ids'])
        {
            $relationProductList = getProductListForIds($info['relation_ids']);
        }

        $this->assign('relationProductList', $relationProductList);
        $this->assign('info', $info);
        //设置全局信息
        $title = "编辑";
        $this->getAllNotesType();
        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('editData');

    }
}